package com.nowcoder.community.controller;

import com.nowcoder.community.entity.Comment;
import com.nowcoder.community.entity.DiscussPost;
import com.nowcoder.community.entity.Event;
import com.nowcoder.community.event.EventProducer;
import com.nowcoder.community.service.CommentService;
import com.nowcoder.community.service.DiscussPostService;
import com.nowcoder.community.util.CommunityConstant;
import com.nowcoder.community.util.HostHolder;
import com.nowcoder.community.util.RedisKeyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Date;

/**
 * 评论
 *
 * @author xiexu
 * @create 2022-06-05 16:16
 */
@Controller
@RequestMapping("/comment")
public class CommentController implements CommunityConstant {

    @Autowired
    private CommentService commentService;

    @Autowired
    private HostHolder hostHolder;

    @Autowired
    private EventProducer eventProducer;

    @Autowired
    private DiscussPostService discussPostService;

    @Autowired
    private RedisTemplate redisTemplate;

    // 添加评论
    @RequestMapping(path = "/add/{discussPostId}", method = RequestMethod.POST)
    public String addComment(@PathVariable("discussPostId") int discussPostId, Comment comment) {
        comment.setUserId(hostHolder.getUser().getId());
        comment.setStatus(0);
        comment.setCreateTime(new Date());
        commentService.addComment(comment);
        // 添加评论以后触发评论事件
        Event event = new Event().setTopic(TOPIC_COMMENT)
                // 当前用户去评论
                .setUserId(hostHolder.getUser().getId()).setEntityType(comment.getEntityType()).setEntityId(comment.getEntityId())
                // 帖子id
                .setData("postId", discussPostId);

        if (comment.getEntityType() == ENTITY_TYPE_POST) { // 如果评论的是帖子
            DiscussPost target = discussPostService.findDiscusspostById(comment.getEntityId());
            event.setEntityUserId(target.getUserId());
            // 计算帖子分数
            String redisKey = RedisKeyUtil.getPostScoreKey();
            redisTemplate.opsForSet().add(redisKey, discussPostId);
        } else if (comment.getEntityType() == ENTITY_TYPE_COMMENT) { // 评论的是评论
            Comment target = commentService.findCommentById(comment.getEntityId());
            event.setEntityUserId(target.getUserId());
        }

        // 发布事件
        eventProducer.fireEvent(event);

        /**
         * 只有评论的是帖子，才触发发帖事件
         * 因为评论帖子后，评论内容发生改变，所以需要重新更新帖子数据到es中
         */
        if (comment.getEntityType() == ENTITY_TYPE_POST) {
            // 触发发帖事件，把新发布的帖子存到es中
            event = new Event()
                    // 事件主题
                    .setTopic(TOPIC_PUBLISH)
                    // 事件的触发人
                    .setUserId(comment.getUserId())
                    // 事件发生的实体类型
                    .setEntityType(ENTITY_TYPE_POST)
                    // 实体id
                    .setEntityId(discussPostId);
            // 发布事件
            eventProducer.fireEvent(event);
        }

        // 重定向到帖子详情页
        return "redirect:/discuss/detail/" + discussPostId;
    }

}
